<template>
    <div class="import-container">
        <el-form ref="form" label-width="120px" v-if="!showStep">
            <el-form-item label="导入模板:">
                <a id="uoloadtemplate" :href="templateFileidHref">{{ importExcelData.templateFilename }}</a>
            </el-form-item>
            <el-form-item label="请选择您要导入的Excel文件:">
                <upload-excel-component @success="handleSuccess" :before-upload="beforeUpload" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="importData">下载导入模板</el-button>
            </el-form-item>
            <el-form-item label-width="0">
                <font color="red">
                    注：<br>
                    1. 上传excel表格的第一行为标题行，数据从第二行开始。<br>
                    2. 为保证数据准确，请将上传表格的工号、姓名、部门的数据格式设置为文本格式，<br>导入日期设置为文本格式，导入金额设置为数值格式。<br>
                    3. 导入数据中，不能出现空行，且工号不能为空值。<br>
                </font>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="nextStep">下一步</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>
        <import-data2 :form-field="formField" :fileid="fileid" :import-excel-data="importExcelData" v-else/>
    </div>
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import importData2 from './importData2'
import { getFormField, exportField } from '@/api/api'
import Cookies from 'js-cookie'
const session = JSON.parse(Cookies.get('sessionData'))
export default {
    name: 'UploadExcel',
    components: { UploadExcelComponent, importData2 },
    props: {
        importExcelData: Object
    },
    data() {
        return {
            formField: [],
            showStep: false,
            fileid: ''
        }
    },
    computed: {
        templateFileidHref() {
            return `/myehr/form/getfile.action?fileId=${this.importExcelData.templateFileid}`
        }
    },
    created() {
        this.init()
    },
    methods: {
        init() {
            getFormField(this.importExcelData.formId)
                .then(res => {
                    console.log(res)
                    this.formField = res
                    for (var j = 0; j < this.formField.length; j++) {
                        if (this.formField[j] === '' || this.formField[j] === null || typeof (this.formField[j]) === undefined) {
                            this.formField.splice(j, 1)
                            j = j - 1
                        }
                    }
                })
        },
        nextStep() {
            if (!this.fileid) {
                this.$notify.error({
                    title: '错误',
                    message: '请上传文件'
                })
                return
            }
            this.showStep = true
        },
        importData() {
            const fieldJson = {}
            for (let i = 0; i < this.formField.length; i++) {
                const fieldCode = this.formField[i].fieldTalbename
                const fieldCodeCheck = fieldCode + '_check'
                fieldJson[fieldCode] = 'Y'
            }
            this.exportData(fieldJson)
        },
        exportData(fieldArray) {
            var _filterdata = {
                buttonId: this.importExcelData.buttonId,
                formId: this.importExcelData.formId,
                userIds: session.userid,
                fieldArray
            }
            exportField(_filterdata)
                .then(text => {
                    console.log(text)
                    if (text[0] === 0) {
                        alert('导出失败！')
                    } else if (text[0] === 'error') {
                        alert('操作异常')
                    } else if (text[0] === 'BIG') {
                        window.open(`/myehr/form/getExportBigFile.action?buttonId=${this.importExcelData.buttonId}`)
                    } else if (text[0] === 'SMALL') {
                        window.open(`/myehr/form/getExportFile.action?buttonId=${this.importExcelData.buttonId}`)
                    }
                })
        },
        beforeUpload(file) {
            const isLt1M = file.size / 1024 / 1024 < 1
            if (isLt1M) {
                return true
            }
            this.$message({
                message: 'Please do not upload files larger than 1m in size.',
                type: 'warning'
            })
            return false
        },
        handleSuccess(e) {
            console.log(e)
            if (e) {
                this.fileid = e.fileid
            }
        }
    }
}
</script>

